"""
@File    : ConnectJira.py
@Time    : 2019/9/9 15:00
@Author  : LiuFeiYu
@Email   : liufeiyu@sunrise.net
@Software: PyCharm
"""
import jira

from Settings.Config import Config


def all_project(jr_obj):
    """登录用户所有项目"""
    return jr_obj.projects()


def create_issue(jr_obj, project_id, summary, description, assignee, priority="3", issuetype="10004"):
    """
    创建问题单
    :param jr_obj: jira实例化对象
    :param project_id: 项目id
    :param summary: 问题单主题
    :param description: 问题单描述
    :param assignee: 经办人(问题指派人)
    :param issuetype: 问题类型,默认10004(故障)
    :param priority:问题优先级
    :return:问题单ID
    """
    field = {
        'project': {'id': project_id},  # 项目
        'issuetype': {'id': issuetype},  # 问题类型
        'priority': {'id': priority},  # 优先级 1High, 2Highest, 3Medium, 4Low, 5Lowest
        'summary': summary,  # 问题主题
        'assignee': {'name': assignee},  # 经办人
        'description': description  # 问题描述
        # 'customfield_11901': test_svn, # 测试版本SVN号
        # 'customfield_10002': test_env, # 测试环境
        # 'fixVersions': [{'name': fix_version}],  # 修复版本
        # 'versions': [{'name': versions}],  # 影响版本
        # 'components': [{'name': component}],  # 相关模块
        # 'customfield_12223': [labels],  # 功能标签
    }
    return jr_obj.create_issue(fields=field)


if __name__ == '__main__':
    # _jr_url = Config().jira_url  # jira地址
    # _jr_username = Config().jira_username  # jira用户名
    # _jr_password = Config().jira_password  # 密码
    # jr_server = jira.JIRA(server=_jr_url,
    #                       basic_auth=(_jr_username, _jr_password))
    # project_res = all_project(jr_server)
    # print(project_res)
    # new_dict = {}
    # for project in project_res:
    #     new_dict[project.key] = project.id
    # print(new_dict)
    pass